/* 
 * StikiWeb Wiki - a Wiki for Hierarchical Organizations
 * $Id: LoadPageAction.java,v 1.7 2006/07/20 21:17:09 biglee Exp $
 * 
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the StikiWeb Wiki 
 *
 * The Initial Developer of the Original Code is Lee Haslup (biglee@haslups.com).
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** 
 */
package org.stikiweb.struts;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.stikiweb.contentMagnager.PageManager;
import org.stikiweb.contentMagnager.PageManagerException;
import org.stikiweb.contentMagnager.PermissionManager;
import org.stikiweb.translations.StikiWebTranslator;

/**
 * @author Allen L (BigLee) Haslup
 *
 */
public class LoadPageAction extends StikiAction {
	
	protected static final int TRAIL_SIZE = 4;
	
	public ActionForward execute(
											ActionMapping mapping, 
											ActionForm form, 
											HttpServletRequest req, 
											HttpServletResponse resp) 
	{
		setup(req);
		HttpSession session = req.getSession();
		HashMap vars = (HashMap)session.getAttribute("vars");
		if(vars==null) {
			vars = new HashMap();
			session.setAttribute("vars",vars);
		}
		Enumeration eNames = req.getParameterNames();
		while(eNames.hasMoreElements()) {
			String paramName = (String)eNames.nextElement();
			if(paramName.startsWith("var_")) {
				String varName = paramName.substring(4);
				String varValue = req.getParameter(paramName);
				vars.put(varName,varValue);
			}
		}
		
		PageBean pb = (PageBean)form;
		try {
			PageManager pm = PageManager.solo();
			// gotta get that page name somewhere...
			String name = pb.getName();
			if(name==null||name.length()<1) {
				name=(String)session.getAttribute("currentPage");
				if(name==null||name.length()<1){
					name="Main";
				}
			}
			name = StikiWebTranslator.crushName(name);
			pb.setName(name);
			// special case page names used for link examples.
			if(name.startsWith("NullPage")) {
				return mapping.findForward("failure");
			}
			if(PermissionManager.solo().userPermLevelForPage(getUserName(req),name) < PermissionManager.READ) {
				if(this.isLoggedIn(req)) {
					return mapping.findForward("failure");
				}
				else {
					return goLoginFirst("viewPage","",name,req);
				}
			}
			req.getSession().setAttribute("currentPage",name);
			// update the "_pageTrail" attribute
			Vector pageTrail = (Vector)req.getSession().getAttribute("pageTrail");
			int oldIndex = -1;
			if(pageTrail==null) {
				pageTrail=new Vector();
				req.getSession().setAttribute("pageTrail",pageTrail);
			}
			else {
				Iterator it = pageTrail.iterator();
				int index = 0;
				while(it.hasNext() && oldIndex<0) {
					if(name.equals((String)it.next())) {
						oldIndex=index;
					}
					index++;
				}
			}
			if(oldIndex>=0)pageTrail.remove(oldIndex);
			if(pageTrail.size()>TRAIL_SIZE) pageTrail.remove(0);
			pageTrail.add(name);
			
			String templateName = PermissionManager.solo().templateForPage(name,req);
			HashMap ancestors = new HashMap();
			ancestors.put(templateName,templateName);
			req.setAttribute("ancestors",ancestors);
			String page = pm.getPage(templateName);
			pb.setText(new StikiWebTranslator().translateText(page, name, templateName, req));
			resp.setContentType("text/html");
			return mapping.findForward("success"); 
		} catch (PageManagerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return mapping.findForward("failure");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return mapping.findForward("failure");
		}
	}

}
